package jdbc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import jdbc.dao.ShopDao;
import jdbc.entity.ShopEntity;

public class ShopDaoImpl implements ShopDao {
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Override
	public void addShop(ShopEntity sName) {
		String sql = "insert into t_jdbc_temp (s_name,s_number,s_price,create_name,update_name) values (?,?,?,?,?)";
		getJdbcTemplate().update(sql, sName.getsName(), sName.getsNumber(), sName.getsPrice(), sName.getCreateName(),sName.getCreateName());

	}

	@Override
	public int deleteShop(String id) {
		String sql = "update t_jdbc_temp set delete_logo='Y' where s_name=?";
		return getJdbcTemplate().update(sql, id);
	}

	@Override
	public int updateShop(ShopEntity id) {
		String sql = "update t_jdbc_temp set s_name=?,s_number=?,s_price=?,create_name=? where s_name=?";
		return getJdbcTemplate().update(sql, id.getsName(), id.getsNumber(),id.getsPrice(),id.getCreateName(),id.getsName());
	}

	@Override
	public ShopEntity getShop(String sNumber) {
		String sql = "select * from t_jdbc_temp where s_name=?";
		try {
			// 如果没有查询到数据会报异常EmptyResultDataAccessException
			return getJdbcTemplate().queryForObject(sql, new String[] { sNumber }, new ShopRowMapper());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<ShopEntity> listShop() {
		String sql = "select * from t_jdbc_temp";
		return getJdbcTemplate().query(sql, new ShopRowMapper());
	}

	@Override
	public int count() {
		String sql = "select count(1) from t_jdbc_temp";
		return getJdbcTemplate().queryForObject(sql, Integer.class);
	}

	class ShopRowMapper implements RowMapper<ShopEntity> {
		// rs为返回结果集，以每行为单位封装着
		public ShopEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
			ShopEntity shop = new ShopEntity();
			shop.setId(rs.getLong("id"));
//			user.setGoodsName(rs.getString("goods_name"));
//			user.setGoodsCode(rs.getString("goods_code"));
//			user.setPrice(rs.getInt("price"));
//			user.setCraeteTime(new Date(rs.getTimestamp("create_time").getTime()));
			shop.setsName(rs.getString("s_name"));
			shop.setsNumber(rs.getString("s_number"));
			shop.setsPrice(rs.getString("s_price"));
			shop.setDeleteLogo(rs.getString("delete_logo"));
			shop.setCreateTime(rs.getTimestamp("create_time"));
			shop.setCreateName(rs.getString("create_name"));
			shop.setUpdateTime(rs.getTimestamp("update_time"));
			shop.setUpdateName(rs.getString("update_name"));
			Timestamp updateTime = rs.getTimestamp("update_time");
			if (updateTime != null) {
				shop.setUpdateTime(new Date(updateTime.getTime()));
			}
			return shop;
		}
	}

}
